﻿namespace CatchBall
{
    using System;

    public class Player
    {
        // initializer 
        public Player()
        {
            this.Col = 1;
            this.Row = 5;
        }

        // Now I can access the row and column of the ball by ball.Row and ball.Col, but only the class can change them
        public int Row { get; private set; }

        public int Col { get; private set; }

        // Deletes the ball from its previous position
        public void VisualizePlayer()
        {
            var matrix = new char[2, 2];
            matrix[0, 0] = '╔';
            matrix[0, 1] = '╗';
            matrix[1, 0] = '╚';
            matrix[1, 1] = '╝';
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(this.Row - 1, this.Col - 1);
            Console.Write(matrix[0, 0]);
            Console.SetCursorPosition(this.Row + 1, this.Col - 1);
            Console.Write(matrix[0, 1]);
            Console.SetCursorPosition(this.Row - 1, this.Col + 1);
            Console.Write(matrix[1, 0]);
            Console.SetCursorPosition(this.Row + 1, this.Col + 1);
            Console.Write(matrix[1, 1]);
            Console.ResetColor();
        }

        // realizes players actions
        public void ListenKey(Ball ball, Game game)
        {
            var rowToClear = this.Row;
            var colToClear = this.Col;

            var key = Console.ReadKey();

            if (key.Key.Equals(ConsoleKey.LeftArrow))
            {
                this.Row -= 2;
            }

            if (key.Key.Equals(ConsoleKey.RightArrow))
            {
                this.Row += 2;
            }

            if (key.Key.Equals(ConsoleKey.UpArrow))
            {
                this.Col -= 2;
            }

            if (key.Key.Equals(ConsoleKey.DownArrow))
            {
                this.Col += 2;
            }

            if (key.Key.Equals(ConsoleKey.Spacebar))
            {
                if (this.Row == ball.Row && this.Col == ball.Col)
                {
                    game.Score++;
                    game.VisualizeScore();
                }
            }

            this.KeepInBoundaries();
            ClearPreviousPlayer(rowToClear, colToClear);
            this.VisualizePlayer();
        }

        // prints the player on its current position
        private static void ClearPreviousPlayer(int row, int col)
        {
            Console.SetCursorPosition(row - 1, col - 1);
            Console.Write(' ');
            Console.SetCursorPosition(row + 1, col - 1);
            Console.Write(' ');
            Console.SetCursorPosition(row - 1, col + 1);
            Console.Write(' ');
            Console.SetCursorPosition(row + 1, col + 1);
            Console.Write(' ');
        }

        private void KeepInBoundaries()
        {
            if (this.Row < 1)
            {
                this.Row += 2;
            }

            if (this.Row > 59)
            {
                this.Row -= 2;
            }

            if (this.Col < 1)
            {
                this.Col += 2;
            }

            if (this.Col > 19)
            {
                this.Col -= 2;
            }
        }
    }
}
